<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      text-decoration: none;
      color:red;
    }
  </style>
</head>
<body>
  <div id="app">

    <h2>今天开心的学路由了</h2>
    <!-- router-link组件用来切换vue路由的 -->
    <router-link to="/home">首页</router-link>
    <router-link to="/news">新闻</router-link>
    <router-link to="/about">关于我们</router-link>
    <hr>
    <!-- 定义一个 出口组件 告诉 路由组件在这里渲染 -->
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 定义三个组件对象
    const Home = {
      template: `
        <div>
          <h2>我是home页</h2>
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h2>我是新闻页</h2>
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>我是关于我们页</h2>
        </div>
      `
    }
    // 定义路由规则（什么path对应什么组件），此时这些路由对象会注册会路由组件
    const routes =  [
        {
          path: '/home',
          component: Home
        },
        {
          path: '/news',
          component: News,
          alias: '/a'
        },
        {
          path: '/about',
          component: About
        }
      ]
    // 创建一个路由对象
    const router = new VueRouter({
      // routes定义路由规则
      routes
    })
    // 将router对象 挂载到 vm上
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>
</html>